Xbasic

STUFF Function

Syntax

Output_String as C = STUFF(C character,N start_position,N number_of_characters,C insert_string)

Arguments

character

The character string to be modified.

start_position

The position within a character string to begin the replacement of characters. Numeric

number_of_characters

The number of characters to replace in a character string. If the Number_Of_Characters parameter is 0, Alpha Anywhere inserts the Insert_String at the given start_position. Numeric

insert_string

The character string to insert into a character string. If Insert_String is empty, the specified Number_Of_Characters is deleted at the start_position.

Description

Alters a string by deleting characters and replacing them with another string.

Discussion

Replaces the specified Number_Of_Characters in a character string, beginning at the specified start_position, with the Insert_String.

Example

STUFF() enables you to insert a string within another string, without the extra steps involved in dividing the target string into two pieces (using the SUBSTR() command), and then reconstructing it by concatenating the two original pieces with the new string.

stuff("Let them eat duck", 14, 4, "cake") -> "Let them eat cake"

Here is how the expression works:

1

The initial string: " Let them eat duck"

2

The 14th character in the string: "d"

3

The string with four characters removed: "Let them eat "

4

The final string: "Let them eat cake"

Here is a more practical example. Assume your table contains a field called FULLNAME, which contains names in the format "Ken Smith." You want to print a report showing each person's first initial and last name (e.g., "K. Smith."). You can define and place a calculated field that uses the following expression:

stuff(FULLNAME, 2, (at(" ", FULLNAME) - 2), ".")

Another expression that achieves the same result is:

substr(FULLNAME, 1, 1) + ". " + " " + word(FULLNAME, 2)

See Also